import 'dart:io';

main () async {
  try {
     print("开始登录");
     String status = await login();
     print(status);
     print("登录成功");
  } catch (e) {
    print(e);
  }
 
}

// 检查用户名 和密码 
// 发送请求
// 拿到结果
// 拿到token同步设置给变量
 Future<String> login () async {
  LoginUser user = LoginUser("admin", "1234567");
  if(user.checkIsEmpty()) {
   print("用户名和密码不能为空");
    return "fail";
  }else {
    // 发送网络请求
    String result = await loginUserAPI(user);
    print(result);
    // 写入
    return "ok";
  }
}

class LoginUser {
  String? username;
  String? password;
  LoginUser(this.username, this.password);
  // 检查是否为空
  bool checkIsEmpty () {
    return   this.username!.isEmpty || this.password!.isEmpty;
  }
}
// 登录的API
Future<String> loginUserAPI (LoginUser user) {
  return Future<String>(() {
    sleep(Duration(seconds: 1));
    if(user.password != "123456") {
      throw Exception("密码错误");
    }
    return "csdcdscsdcdscsdcsdcd";
  });
}